package com.anjubao.parking.municipal.roadside.service;

import com.alibaba.fastjson.JSON;
import com.anjubao.parking.municipal.common.utils.RedisClientTemplate;
import com.anjubao.parking.municipal.common.utils.U;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试专用,主要用redis代替
 *
 * @author zhuchunfeng
 * @date 2019/6/13 15:40
 */
@Service
public class RedisService {

    private static final String COMMAND_QUEUE = "ROADSIDE_SERVICE_COMMAND:device.";

    @Resource
    private RedisClientTemplate redisClientTemplate;

    public void putCmd(String deviceId,String cmd){
        Map<String, Object> param = new HashMap<>();
        param.put("cmd", cmd);
        redisClientTemplate.lLeftPush(COMMAND_QUEUE + deviceId, U.json(param));
//        deviceIdmapCommands.putIfAbsent(deviceId, new ArrayList<>());
//        deviceIdmapCommands.get(deviceId).add(U.json(param));
    }

    public void putCmdWithParam(String deviceId, String cmd, Map<String, String> param) {
        param.put("cmd", cmd);
        redisClientTemplate.lLeftPush(COMMAND_QUEUE + deviceId, U.json(param));
//        deviceIdmapCommands.putIfAbsent(deviceId, new ArrayList<>());
//        deviceIdmapCommands.get(deviceId).add(U.json(param));
    }

    public Map<String,String> getCmd(String deviceId){
        String s = redisClientTemplate.lLeftPop(COMMAND_QUEUE + deviceId);
        if (StringUtils.isEmpty(s)) {
            return new HashMap<>();
        }
        return JSON.parseObject(s, Map.class);

    }

}
